package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.user.entity.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import com.atguigu.gmall.user.vo.LoginSuccessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lhh
 * @ClassName UserController
 * @date 2022 2022/12/14
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    UserInfoService userInfoService;

    @ApiOperation(value = "用户登录校验")
    @PostMapping("/passport/login")
    public Result<LoginSuccessVo> login(@RequestBody UserInfo userInfo){
        LoginSuccessVo user =userInfoService.login(userInfo);
        if(user!=null){
            return Result.ok(user);
        }
        return Result.build(null, ResultCodeEnum.FAIL);
    }

    @ApiOperation("退出登录")
    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader("token") String token){
        userInfoService.logout(token);
        return Result.ok();
    }
}
